package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.LabelClassifyAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.LabelClassifyBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AllClassifyActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    private LabelClassifyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_classify);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void getDatas() {
        OkgoUtil.post(Http.ALL_LABEL,this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LabelClassifyBean parentBean= DealResult.getInstance().dealDatas(AllClassifyActivity.this,response,new TypeToken<LabelClassifyBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS){
                            List<LabelClassifyBean.BodyBean> beans = parentBean.getBody();
                            if(beans.isEmpty()){return;}
                            mAdapter.addAll(beans);
                            recyclerView.refreshComplete(10);
                        }else {
                            if(code==Constant.EMPTY){
                                recyclerView.refreshComplete(mAdapter.getItemCount());
                                ToastUtils.toastForShort(AllClassifyActivity.this,getString(R.string.empty_datas));
                            }else{
                                ToastUtils.toastForShort(AllClassifyActivity.this,parentBean.getResult());
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                        ToastUtils.toastForShort(AllClassifyActivity.this,getString(R.string.bad_net));
                    }
                });
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("全部分类");
        tvMiddleTitle.setVisibility(View.VISIBLE);
        mAdapter = new LabelClassifyAdapter(this);
        mAdapter.setDataList(new ArrayList<LabelClassifyBean.BodyBean>());
        recyclerView.setLayoutManager(new GridLayoutManager(this,4));
         LRecyclerViewAdapter lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        //divider
        GridItemDecoration divider = new GridItemDecoration.Builder(this)
//                .setHorizontal(R.dimen.size_4)
                 .setVertical(R.dimen.size_20)
                .setColorResource(R.color.white)
                .build();
        recyclerView.setHasFixedSize(true);
//        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(false);
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent i=new Intent(AllClassifyActivity.this, ChildClassifyAllActivity.class);
                i.putExtra("id",mAdapter.getDataList().get(position).getLabelclassid()+"");
                i.putExtra("name",mAdapter.getDataList().get(position).getClassname());
                startActivity(i);
                AllClassifyActivity.this.finish();
            }
        });
    }

    @OnClick({R.id.title_left_one_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;

        }
    }
}
